package org.example.Dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.example.Bean.Employee;
import org.example.Dao.IEmployeeDao;
import org.example.Utils.MyBatisUtils;

import java.util.List;

public class EmployeeDao implements IEmployeeDao {

	//这里每一次操作都要打开一个会话，只用一个会话的话，在结束完一个操作的时候，会话就关闭了
	@Override
	public int add(Employee employee) {
		SqlSession session= MyBatisUtils.getSession();
		int count=session.insert("EmployeeMapper.add");
		session.commit();
		session.close();
		return count;
	}

	@Override
	public int update(Employee employee) {
		SqlSession session= MyBatisUtils.getSession();
		int count=session.insert("EmployeeMapper.update");
		session.commit();
		session.close();
		return count;
	}

	@Override
	public int remove(Integer id) {
		SqlSession session= MyBatisUtils.getSession();
		int count=session.delete("EmployeeMapper.delete",id);
		session.commit();
		session.close();
		return count;
	}

	@Override
	public Employee getById(Integer id) {
		SqlSession session= MyBatisUtils.getSession();
		Employee employee=(Employee)session.selectOne("EmployeeMapper.getById",id);
		session.close();
		return employee;
	}

	@Override
	public List<Employee> getAll() {
		SqlSession session= MyBatisUtils.getSession();
		List<Employee> list=session.selectList("EmployeeMapper.getById");
		session.close();
		return list;
	}
}
